from django.db import models


class BaseModel(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='id')
    gmt_created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    gmt_modified = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_allow_editing = models.BooleanField(default=False, verbose_name='是否允许编辑')
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        managed = False


class InspectionInfoBase(BaseModel):
    """巡检信息基类"""
    fault_num = models.CharField(max_length=40, null=True, blank=True, verbose_name='故障编号')

    sn = models.CharField(max_length=100, default='', verbose_name='设备sn')
    out_of_band_ip = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='带外IP')
    business_ip = models.TextField(null=True, blank=True, verbose_name='业务IP')
    material_type = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='类型')
    manufacturer = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='厂商')
    model_specifications = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='型号')
    source_computer_room = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在机房')
    source_building = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在楼栋')
    source_compartment = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在包间')
    source_cabinet = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在机柜')
    source_frame_info = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在机位')
    product_line = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='产品线')
    product_line_id = models.IntegerField(null=True, blank=True, verbose_name="产线ID")
    user_name = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='使用人')

    inspect_person = models.CharField(max_length=100, default='', verbose_name='巡检人员')
    inspect_time = models.DateTimeField(null=True, blank=True, verbose_name="巡检日期")
    exception_description = models.CharField(max_length=500, default='', verbose_name='故障分类')
    exception_flag = models.CharField(max_length=100, default='', verbose_name='巡检标记')  # 增量/存量/销项

    verify_person = models.CharField(max_length=100, default='', verbose_name='确认人员')
    verify_time = models.DateTimeField(null=True, blank=True, verbose_name="确认日期")
    exception_check = models.CharField(max_length=20, default='', verbose_name='故障是否属实')
    fault_slot = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障槽位')
    fault_type = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障类型')
    fault_model = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障型号')
    fault_sn = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障件SN')
    fault_details = models.TextField(null=True, blank=True, verbose_name="故障详情")
    alarm_level = models.CharField(max_length=64, default='', null=True, blank=True, verbose_name='告警等级')

    fault_remarks = models.TextField(null=True, blank=True, verbose_name="故障备注")

    class Meta:
        managed = False
        abstract = True


class InspectionInfoList(models.Model):
    """巡检信息库"""
    fault_num = models.CharField(max_length=40, null=True, blank=True, verbose_name='故障编号')

    sn = models.CharField(max_length=100, default='', verbose_name='设备sn')
    out_of_band_ip = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='带外IP')
    business_ip = models.TextField(null=True, blank=True, verbose_name='业务IP')
    material_type = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='类型')
    manufacturer = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='厂商')
    model_specifications = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='型号')
    source_computer_room = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在机房')
    source_building = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在楼栋')
    source_compartment = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在包间')
    source_cabinet = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在机柜')
    source_frame_info = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='所在机位')
    product_line = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='产品线')
    product_line_id = models.IntegerField(null=True, blank=True, verbose_name="产线ID")
    user_name = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='使用人')

    inspect_person = models.CharField(max_length=100, default='', verbose_name='巡检人员')
    inspect_time = models.DateTimeField(null=True, blank=True, verbose_name="巡检日期")
    exception_description = models.CharField(max_length=500, default='', verbose_name='故障分类')
    exception_flag = models.CharField(max_length=100, default='', verbose_name='巡检标记')  # 增量/存量/销项

    verify_person = models.CharField(max_length=100, default='', verbose_name='确认人员')
    verify_time = models.DateTimeField(null=True, blank=True, verbose_name="确认日期")
    exception_check = models.CharField(max_length=20, default='', verbose_name='故障是否属实')
    fault_slot = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障槽位')
    fault_type = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障类型')
    fault_model = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障型号')
    fault_sn = models.CharField(max_length=100, default='', null=True, blank=True, verbose_name='故障件SN')
    fault_details = models.TextField(null=True, blank=True, verbose_name="故障详情")
    alarm_level = models.CharField(max_length=64, default='', null=True, blank=True, verbose_name='告警等级')

    fault_remarks = models.TextField(null=True, blank=True, verbose_name="故障备注")

    first_find_time = models.DateTimeField(null=True, blank=True, verbose_name="首次发现日期")
    close_time = models.DateTimeField(null=True, blank=True, verbose_name="故障消除时间")

    status = models.IntegerField(default=0, verbose_name="巡检信息状态")  # 0.待处理, 1.处理中, 2.已消除

    is_isolate = models.BooleanField(default=False, verbose_name="是否屏蔽")

    # 1. 人工巡检; 2.系统触发, 3. 外地输入, 4. 带外平台
    source = models.IntegerField(default=1, null=True, blank=True, verbose_name="巡检来源")

    maintenance_result = models.CharField(max_length=20, null=True, blank=True, verbose_name='维护结果')
    maintenance_person = models.CharField(max_length=64, null=True, blank=True, verbose_name='维护人员')
    maintenance_time = models.DateTimeField(null=True, blank=True, verbose_name="维护日期")

    close_record = models.CharField(max_length=64, null=True, blank=True, verbose_name='销项记录')

    remarks = models.TextField(null=True, blank=True, verbose_name="备注")

    user_remarks = models.TextField(null=True, blank=True, verbose_name="用户备注")

    gmt_created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    gmt_modified = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_allow_editing = models.BooleanField(default=False, verbose_name='是否允许编辑')
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        managed = False
        ordering = ['-inspect_time']
        db_table = 'InspectionInfoList'


class ConfigOptions(models.Model):
    """下拉选项配置字典"""
    module = models.CharField(max_length=40, null=True, blank=True, verbose_name="模块")
    config_type = models.CharField(max_length=64, verbose_name="配置类型")
    label = models.CharField(max_length=64, verbose_name="选项名称")
    code = models.CharField(max_length=64, null=True, blank=True, verbose_name="选项编码")
    value = models.CharField(max_length=64, verbose_name="选项值")
    remarks = models.CharField(max_length=256, null=True, blank=True, verbose_name="备注")
    enabled = models.BooleanField(default=False, verbose_name="是否启用")

    tag = models.CharField(max_length=64, null=True, blank=True, verbose_name="标签")
    group = models.CharField(max_length=64, null=True, blank=True, verbose_name="分组")

    create_user = models.CharField(max_length=64, null=True, blank=True, verbose_name="创建人")
    gmt_created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    gmt_modified = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_allow_editing = models.BooleanField(default=False, verbose_name='是否允许编辑')
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        managed = False
        ordering = ['-gmt_modified']
        db_table = 'ConfigOptions'


class AssetParamDict(models.Model):
    """资产参数字典"""

    big_category = models.CharField(max_length=40, null=True, blank=True, verbose_name="设备分类")
    category = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产分类")
    model = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产型号")
    param = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产参数")

    manufacturer = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产厂商")
    supplier = models.CharField(max_length=64, null=True, blank=True, verbose_name="供应商")

    class Meta:
        managed = False
        db_table = 'AssetParamDict'


class CustomUserColumns(models.Model):
    """针对用户自定义机柜表表头"""
    username = models.CharField(max_length=128, null=True, blank=True, verbose_name="用户名")
    category = models.CharField(max_length=128, null=True, blank=True, verbose_name="类型")
    columns = models.TextField(null=True, blank=True, verbose_name="表头")

    createTime = models.DateTimeField(auto_now_add=True)
    updateTime = models.DateTimeField(auto_now=True)

    class Meta:
        managed = False
        db_table = 'CustomUserColumns'


class ResourcePhysicalMachine(models.Model):
    """在线物理机"""

    # 基本信息
    systemSN = models.CharField(unique=True, max_length=64, null=True, blank=True, verbose_name="资产SN")
    bigCategory = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产大类")
    assetType = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产类型")
    secondSn = models.CharField(max_length=64, null=True, blank=True, verbose_name='厂商编码')
    manu = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产厂商")
    model = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产型号")
    status = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产状态")
    workingStatus = models.CharField(max_length=64, null=True, blank=True, verbose_name="运行状态")
    manageIp = models.CharField(max_length=255, null=True, blank=True, verbose_name="管理IP")
    iopsSystemIp = models.CharField(max_length=64, null=True, blank=True, verbose_name="IOPS同步系统IP")
    otherIp = models.TextField(null=True, blank=True, verbose_name="其它IP")
    ipmi = models.CharField(max_length=255, null=True, blank=True, verbose_name="带外IP")
    netEnv = models.CharField(max_length=64, null=True, blank=True, verbose_name="网络环境")
    networkArea = models.CharField(max_length=64, null=True, blank=True, verbose_name="网络区域")
    arch = models.CharField(max_length=64, null=True, blank=True, verbose_name="架构")
    devicePurpose = models.CharField(max_length=64, null=True, blank=True, verbose_name="设备用途")
    diskPosition = models.IntegerField(null=True, verbose_name="盘位")
    supplier = models.CharField(max_length=128, null=True, blank=True, verbose_name="供应商")
    singlePower = models.CharField(max_length=32, null=True, blank=True, verbose_name="单电源")
    factoryConfig = models.CharField(max_length=512, null=True, blank=True, verbose_name="出厂配置")
    briefConfig = models.CharField(max_length=512, null=True, blank=True, verbose_name="简要配置")

    # 管理信息
    dataCenter = models.CharField(max_length=64, null=True, blank=True, verbose_name="数据中心")
    availableZone = models.CharField(max_length=64, null=True, blank=True, verbose_name="可用区")
    roomCode = models.CharField(max_length=64, null=True, blank=True, verbose_name="机房编码")
    rackBuilding = models.CharField(max_length=64, null=True, blank=True, verbose_name="所在楼栋")
    rackFloor = models.CharField(max_length=64, null=True, blank=True, verbose_name="所在楼层")
    rackModule = models.CharField(max_length=64, null=True, blank=True, verbose_name="所在模块")
    rackCode = models.CharField(max_length=64, null=True, blank=True, verbose_name="所在机柜号")
    uLocationStart = models.IntegerField(null=True, verbose_name="U位起始")
    uLocationEnd = models.IntegerField(null=True, verbose_name="U位结束")
    goodsBelong = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产归属")
    isChangeConfig = models.CharField(max_length=64, null=True, blank=True, verbose_name="是否改配")
    uptime = models.DateTimeField(null=True, verbose_name="最新上架时间")
    downTime = models.DateTimeField(null=True, verbose_name="最新下架时间")
    GPUcardType = models.CharField(max_length=64, null=True, blank=True, verbose_name="GPU卡型")
    griffSn = models.CharField(max_length=64, null=True, blank=True, verbose_name="刀箱SN")
    griffSlot = models.IntegerField(null=True, verbose_name="刀箱槽位")
    businessOrder = models.CharField(max_length=64, null=True, blank=True, verbose_name="关联工单")
    userDivision = models.CharField(max_length=64, null=True, blank=True, verbose_name="使用人BG")
    userProductLine = models.CharField(max_length=128, null=True, blank=True, verbose_name="使用人产线")
    userProductLineId = models.CharField(max_length=64, null=True, blank=True, verbose_name="使用人产线ID")
    userAccount = models.CharField(max_length=64, null=True, blank=True, verbose_name="使用人域账号")
    cnName = models.CharField(max_length=64, null=True, blank=True, verbose_name="使用人中文名")
    applyUserDivision = models.CharField(max_length=64, null=True, blank=True, verbose_name="申请人BG")
    applyUserProductLine = models.CharField(max_length=64, null=True, blank=True, verbose_name="申请人产线")
    applyUserProductLineId = models.CharField(max_length=64, null=True, blank=True, verbose_name="申请人产线ID")
    applyUserAccount = models.CharField(max_length=64, null=True, blank=True, verbose_name="申请人域账号")
    applyUserCnName = models.CharField(max_length=64, null=True, blank=True, verbose_name="申请人中文名")
    remark = models.CharField(max_length=1024, null=True, blank=True, verbose_name="备注")
    applyScene = models.CharField(max_length=128, null=True, blank=True, verbose_name="应用场景")
    isDomestic = models.CharField(max_length=32, null=True, blank=True, verbose_name="是否国产化")
    bmcAdmin = models.CharField(max_length=128, null=True, blank=True, verbose_name="bmcAdmin")
    bmcUser = models.CharField(max_length=128, null=True, blank=True, verbose_name="bmcUser")
    poolState = models.CharField(max_length=64, null=True, blank=True, verbose_name="资源池状态")

    # 采购信息
    costCenter = models.CharField(max_length=128, null=True, blank=True, verbose_name="成本中心")
    price = models.FloatField(null=True, verbose_name="采购单价（含税）")
    nowPrice = models.FloatField(null=True, verbose_name="实时价格")
    warrantySince = models.DateField(null=True, verbose_name="维保开始时间")
    warrantyEnd = models.DateField(null=True, verbose_name="维保结束时间")
    assetCode = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产编码")
    arrivalDate = models.DateField(null=True, verbose_name="到货日期")
    purchaseOrderNum = models.CharField(max_length=64, null=True, blank=True, verbose_name="采购单号")
    belongCompany = models.CharField(max_length=128, null=True, blank=True, verbose_name="归属公司")
    purchaseProject = models.CharField(max_length=128, null=True, blank=True, verbose_name="采购项目")

    is_deleted = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now=True)
    update_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        managed = False
        db_table = 'ResourcePhysicalMachine'
